<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of MobileService
 *
 * @author Administrator
 */
import('@.Service.TypeService');
import('@.Service.AppService');
import('@.Service.ConfigService');

class MobileService {
    //获取所有的Type信息

    /**
     * 就是data下面先分三个大类做key，内容是他们下的小类，每个小类名做key：内容是小类的详细信息。
     * t_id,count,free_count,app_icon
     */
    function listType($field = 'app.*') {
        $result = S(ConstantUtil::$SERVICE_MOBILESERVICE_LISTTYPE);
//        if (!empty($result)) {
//            return $result;
//        }

        $typeService = new TypeService();
        $appService = new AppService();
        $configService = new ConfigService();

        $rootType = $typeService->listRootTypes();

        $result = array();
        //过滤 手机上不显示的 类型
        $mobileFilterType = split(',', $configService->getConfigValue('mobile_filter_type_name'));
        foreach ($rootType as $root) {
            if ('软件类型' == $root['name']) {
                continue;
            }
            $child = $typeService->listChildTypesByRootTypeId($root['id']);

            $childType = array();


            foreach ($child as $type) {
                if (-1 < array_search($type['name'], $mobileFilterType)) {
                    continue;
                }

                $count = $appService->countAppBySmallTypeName($type['name'], null);
                $app = $appService->findFirstTypeAppById($type['id'], $field);
                $free_count = $appService->countFreeAppByTypeId($type['id']);

//                array_push($childType, array('key' => $type['name'], 'value' => array('t_id' => $type['id'], 'count' => $count, 'free_count' => $free_count, 'app_icon' => $app['app_icon'])));
                array_push($childType, array('name' => $type['name'], 'app_icon' => $app['app_icon'], 'count' => $count, 'free_count' => $free_count, 't_id' => $type['id']));
            }

            array_push($result, array('key' => $root['name'], 'value' => $childType));
        }

        //放入缓存
        S(ConstantUtil::$SERVICE_MOBILESERVICE_LISTTYPE, $result, ConstantUtil::$CACHE_TIME);
        return $result;
    }

    function insert($mobile) {
        D('mobile')->add($mobile);
    }

    function update($mobile) {
        D('mobile')->save($mobile);
    }

    function findMobileByToken($token) {
        $where['token'] = array('eq', $token);
        return D('mobile')->where($where)->find();
    }

}

?>
